package controllers

import (
	"docker/models"
	beego "github.com/beego/beego/v2/server/web"
)

type DockerController struct {
	beego.Controller
}

func (c *DockerController) Add() {
	var err error
	name := c.GetString("name")
	image := c.GetString("image")
	tel := c.GetString("tel")
	addr := c.GetString("addr")
	price, _ := c.GetFloat("price")

	dockers := models.Dockers{
		Name:  name,
		Image: image,
		Tel:   tel,
		Addr:  addr,
		Price: price,
	}

	if err = dockers.Add(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"mes":  "添加失败",
		}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"mes":  "添加成功",
	}
	c.ServeJSON()
	return

}
func (c *DockerController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	var dockers []models.Dockers
	err := models.DB.Find(&dockers).Error
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"mes":  "展示失败",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"mes":  "展示成功",
		"data": dockers,
	}
	c.ServeJSON()
	return

}
func (c *DockerController) Update() {
	var err error
	name := c.GetString("name")
	image := c.GetString("image")
	tel := c.GetString("tel")
	addr := c.GetString("addr")
	price, _ := c.GetFloat("price")

	dockers := models.Dockers{
		Name:  name,
		Image: image,
		Tel:   tel,
		Addr:  addr,
		Price: price,
	}

	if err = dockers.Update(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"mes":  "修改失败",
		}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"mes":  "修改成功",
	}
	c.ServeJSON()
	return

}
func (c *DockerController) Del() {
	var err error
	var dockers models.Dockers
	id, _ := c.GetInt("id")
	dockers.ID = uint(id)

	if err = dockers.Del(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"mes":  "删除失败",
		}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"mes":  "删除成功",
	}
	c.ServeJSON()
	return

}
